package sdu.icat.ltp.mongo;

import java.util.ArrayList;
import java.util.List;

import org.bson.Document;

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;

import sdu.icat.ltp.api.TransElement;

public class MongoDbConnection {

    private String baseName;
    private final String location = "192.168.11.101";
    private final String DEFAULT_COL = "document";
    private final MongoClient client = new MongoClient(location,27017);
    private MongoDatabase db;

    /**
     * 默认为ltp_document数据库
     * @param baseName
     */
    public void getConnection(String baseName) {
	if(baseName==null) {
	    this.baseName = "ltp_document";
	} else
	    this.baseName = baseName;
	db = client.getDatabase(this.baseName);
    }

    /**
     * 存储数据
     * @param element
     */
    public void insertText(TransElement element) {
	if(db==null) {
	    getConnection(null);
	}
	String baseName = element.getBaseName();
	if(baseName==null) {
	    baseName = DEFAULT_COL;
	}
	MongoCollection<Document> collection = db.getCollection(baseName);
	if(collection==null) {
	    db.createCollection(baseName);
	    collection = db.getCollection(baseName);
	}
	String title = element.getTitle();
	if(!findExist(title,element)) {
	    int paragraph = element.getParagraph();
	    Document document = new Document("title",title)
		    .append("paragraph", paragraph)
		    .append("format", element.getFormat())
		    .append("text", element.getText())
		    .append("body", element.getBody());
	    collection.insertOne(document);
	}
    }


    /**
     * 批量存储数据
     * @param title
     * @param elements
     */
    public void insertTexts(String title,List<TransElement> elements) {
	insertArticles(DEFAULT_COL,title,elements);
    }
    
    public void insertArticles(String articleTitle,String title,List<TransElement> elements) {
	if(db==null) {
	    getConnection(null);
	}
	MongoCollection<Document> collection = getCollection(articleTitle);
	if(collection==null) {
	    db.createCollection(title);
	    collection = db.getCollection(title);
	}
	List<Document> documents = new ArrayList<>();
	for(TransElement element:elements) {
	    if(!findExist(title,element)) {
		Document document = new Document("title",title)
			.append("paragraph", element.getParagraph())
			.append("format", element.getFormat())
			.append("text", element.getText())
			.append("body", element.getBody());
		documents.add(document);
	    }
	}
	collection.insertMany(documents);
    }

    /**
     * 判断是否存在
     * @param title
     * @param element
     * @return
     */
    public boolean findExist(String title,TransElement element) {
	if(db==null) {
	    getConnection(null);
	}
	MongoCollection<Document> collection = getCollection(DEFAULT_COL);
	Document doc = collection.find(Filters.and(Filters.eq("title",title),Filters.eq("text",element.getText()),Filters.eq("body",element.getBody()))).first();
	if(doc==null)
	    return false;
	return true;
    }

    /**
     * 创建连接
     * @param name
     */
    public void createMongoConnection(String name) {
	getCollection(name);
    }

    /**
     * 获取CollectionName的集合
     * @param collectionName
     * @return
     */
    public MongoCollection<Document> getCollection(String collectionName) {
	if(db==null) {
	    getConnection(null);
	}
	return db.getCollection(collectionName);
    }

    /**
     * 统计集合数量
     * @param collection
     * @return
     */
    public long count(MongoCollection<Document> collection) {
	return collection.count();
    }

    /**
     * 获取集合遍历
     * @param collection
     * @return
     */
    public MongoCursor<Document> getCursor(MongoCollection<Document> collection) {
	return collection.find().iterator();
    }

    /**
     * 关闭连接
     */
    public void closeConnection() {
	client.close();
    }

}
